//
//  ClockView.m
//  ProjectB
//
//  Created by lanou on 16/5/6.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ClockView.h"

@interface ClockView ()

@property (nonatomic, strong) UIView *hourView;
@property (nonatomic, strong) UIView *minuteView;
@property (nonatomic, strong) UIView *secondView;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation ClockView

- (instancetype)initWithFrame:(CGRect)frame date:(NSDate *)date autoRun:(BOOL)isRun {
    _date = date;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:date];
    NSInteger hour = components.hour;
    NSInteger minute = components.minute;
    NSInteger second = components.second;
    return [self initWithFrame:frame hour:hour minute:minute second:second autoRun:isRun];
}

- (instancetype)initWithFrame:(CGRect)frame hour:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second autoRun:(BOOL)isRun {
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.cornerRadius = frame.size.width / 2;
        self.layer.borderWidth = 5;
        
        _minuteView = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width / 2 - 1, frame.size.height / 10, 2, frame.size.height / 5 * 2 + 10)];
        _minuteView.backgroundColor = [UIColor blackColor];
        _minuteView.layer.position = CGPointMake(frame.size.width / 2, frame.size.width / 2);
        _minuteView.layer.anchorPoint = CGPointMake(0.5, (frame.size.height / 5 * 2) / (frame.size.height / 5 * 2 + 10));
        self.minute = minute;
        
        _hourView = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width / 2 - 2, frame.size.height / 4, 4, frame.size.height / 4 + 5)];
        _hourView.backgroundColor = [UIColor blackColor];
        _hourView.layer.position = CGPointMake(frame.size.width / 2, frame.size.width / 2);
        _hourView.layer.anchorPoint = CGPointMake(0.5, (frame.size.height / 4) / (frame.size.height / 4 + 5));
        self.hour = hour;
        
        [self addSubview:_minuteView];
        [self addSubview:_hourView];
        
        UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width / 2 - 2.5, frame.size.height / 2 - 2.5, 5, 5)];
        centerView.backgroundColor = [UIColor redColor];
        centerView.layer.cornerRadius = 2.5;
        [self addSubview:centerView];
        
        if (second >= 0) {
            _secondView = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width / 2 - 1, 15, 1, frame.size.height / 2)];
            _secondView.backgroundColor = [UIColor redColor];
            _secondView.layer.position = CGPointMake(frame.size.width / 2, frame.size.width / 2);
            _secondView.layer.anchorPoint = CGPointMake(0.5, (frame.size.height / 2 - 15) / (frame.size.height / 2));
            [self setFirstSecond:second];
            [self addSubview:_secondView];
            
            _autoRun = isRun;
            if (isRun) {
                _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(run:) userInfo:nil repeats:YES];
            }
        }
    }
    return self;
}

- (void)changeColor {
    self.layer.borderColor = [UIColor colorWithWhite:0.35 alpha:1].CGColor;
    self.backgroundColor = [UIColor colorWithWhite:0.25 alpha:1];
    _minuteView.backgroundColor = [UIColor whiteColor];
    _hourView.backgroundColor = [UIColor whiteColor];
}

- (void)run:(NSTimer *)timer {
    NSDate *date = [[NSDate alloc] init];
    if (_date != date) {
        _date = date;
        
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *components = [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:date];
        NSInteger hour = components.hour;
        NSInteger minute = components.minute;
        NSInteger second = components.second;
        
        self.minute = minute;
        self.hour = hour;
        self.second = second;
    }
}

- (void)setAutoRun:(BOOL)autoRun {
    if (autoRun) {
        [self.timer setFireDate:[NSDate date]];
    } else {
        [self.timer setFireDate:[NSDate distantFuture]];
    }
}

- (void)setDate:(NSDate *)date {
    if (_date != date) {
        _date = date;
        
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *components = [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:date];
        NSInteger hour = components.hour;
        NSInteger minute = components.minute;
        NSInteger second = components.second;
        
        self.minute = minute;
        self.hour = hour;
        [self setFirstSecond:second - 1];
        [self.timer setFireDate:[NSDate date]];
    }
}

- (void)setHour:(NSInteger)hour {
    if (hour < 0 || hour > 24) {
        return;
    }
    
    if (_hour != hour) {
        _hour = hour;
    }
    [UIView animateWithDuration:0.1 animations:^{
        self.hourView.transform = CGAffineTransformMakeRotation((hour + self.minute / 60.0) / 12.0 * M_PI * 2);
    }];
}

- (void)setMinute:(NSInteger)minute {
    if (minute < 0 || minute > 60) {
        return;
    }
    
    if (_minute != minute) {
        _minute = minute;
        [UIView animateWithDuration:0.1 animations:^{
            self.minuteView.transform = CGAffineTransformMakeRotation(minute / 60.0 * M_PI * 2);
        }];
    }
}

- (void)setSecond:(NSInteger)second {
    if (second < 0 || second > 60) {
        return;
    }
    
    if (_second != second) {
        _second = second;
        [UIView animateWithDuration:1 animations:^{
            [UIView setAnimationCurve:UIViewAnimationCurveLinear];
            self.secondView.transform = CGAffineTransformMakeRotation(second / 60.0 * M_PI * 2);
        }];
    }
}

- (void)setFirstSecond:(NSInteger)second {
    if (second >= 0 && second <= 60) {
        _second = second;
        [UIView animateWithDuration:0.1 animations:^{
            [UIView setAnimationCurve:UIViewAnimationCurveLinear];
            self.secondView.transform = CGAffineTransformMakeRotation(second / 60.0 * M_PI * 2);
        }];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
